<?php

namespace app\shop\model\order;

use app\shop\enums\order\refund\RefundAuditStatus;
use app\shop\enums\order\refund\RefundStatus;
use app\shop\enums\order\refund\RefundType;
use app\shop\enums\order\refund\RefundUserShipStatus;
use app\shop\model\BaseModel;
use think\model\relation\BelongsTo;
use think\model\relation\HasOne;

class OrderRefundModel extends BaseModel
{
    protected $name = 'shop_order_refund';

    /**
     * 设置字段类型
     * @return string[]
     */
    public function setType(): array
    {
        return [
            'refund_address'     => 'json',
            'user_ship_delivery' => 'json',
            'exchange_delivery'  => 'json'
        ];
    }

    /**
     * 关联订单
     * @return BelongsTo
     * @noinspection PhpUnused
     */
    public function orderRelation(): BelongsTo
    {
        return $this->belongsTo(OrderModel::class, 'order_id', 'id');
    }

    /**
     * 关联订单详情
     * @return BelongsTo
     */
    public function detail(): BelongsTo
    {
        return $this->belongsTo(OrderDetailModel::class, 'order_detail_id', 'id');
    }

    /**
     * 关联售后地址
     * @return HasOne
     */
    public function address(): HasOne
    {
        return $this->hasOne(OrderRefundAddressModel::class, 'refund_order_id', 'id');
    }

    /**
     * 获取器：售后状态
     * @param $value
     * @param $data
     * @return string
     * @noinspection PhpUnused
     * @noinspection PhpUnusedParameterInspection
     */
    public function getStatusTextAttr($value, $data): string
    {
        $refundTypes = RefundType::getAllDescriptions();

        if ($data['status'] == RefundStatus::CANCEL) {
            return '售后申请已取消';
        }
        if ($data['audit_status'] == RefundAuditStatus::AUDIT_REJECT) {
            return $refundTypes[$data['refund_type']] . '申请已被拒绝';
        }
        // 进行中状态
        if ($data['audit_status'] == RefundAuditStatus::WAIT_AUDIT) {
            return $refundTypes[$data['refund_type']] . '申请审核中';
        }
        if ($data['status'] == RefundStatus::COMPLETED) {
            return '已完成' . $refundTypes[$data['refund_type']];
        }

        return $data['user_ship_status'] == RefundUserShipStatus::SHIPPED ? '已发货,等待商家确认' : '已同意,等待用户退货';
    }
}